/*
 * thread.h
 *
 *  Created on: 2008-9-26
 *      Author: fanliangliang
 */

#ifndef TEST_THREAD_H_
#define TEST_THREAD_H_

#include "toxic.h"

using namespace toxic;

//no argument function
static void print() {
	cout << "print" << endl;
}

//unary function
static void print1(int n) {
	cout << "print1 " << n << endl;
}

//binary function
static void print2(int m, int n) {
	cout << "print2 " << m << " " << n << endl;
}

//no argument functor
struct PrintFunctor {
	void operator()() const {
		cout << "PrintFunctor" << endl;
	}
};

//unary functor
struct PrintFunctor1 {
	typedef void result_type;
	void operator()(int n) const {
		cout << "PrintFunctor1 " << n << endl;
	}
};

//binary functor
struct PrintFunctor2 {
	typedef void result_type;
	void operator()(int m, int n) const {
		cout << "PrintFunctor2 " << m << " " << n << endl;
	}
};

//inherited from Thread
struct MyThread : public Thread {
	virtual void run() {
		cout << "MyThread" << endl;
	}
};

//"implement" Runnable interface
struct MyRunnable : public Runnable {
	virtual void run() {
		cout << "MyRunnable" << endl;
	}
};


class SimpleClass {
public:
	void print() {
		cout << "TestClass.print" << endl;
	}

	void print(int i) {
		cout << "TestClass.print(" << i << ")" << endl;
	}
};

void do_test_thread() {

	cout << "***** thread *****" << endl;

 	//construct Thread with no argument function
 	Thread thread1(&print);

 	//construct Thread with unary function
 	Thread thread2(bind(&print1, 5));

 	//construct Thread with binary function
 	Thread thread3(bind(&print2, 1, 2));

 	//construct Thread with no argument functor
 	PrintFunctor f;
 	Thread thread4(f);

 	//construct Thread with unary functor
 	Thread thread5(bind(PrintFunctor1(), 5));

 	//construct Thread with binary functor
 	Thread thread6(bind(PrintFunctor2(), 1, 2));

 	MyThread thread7;

 	Thread thread8(new MyRunnable());

 	SimpleClass a;
 	Thread thread9(bind(&SimpleClass::print, a));

 	Thread thread10(bind(&SimpleClass::print, a, 5));


 	thread1.start();
 	thread2.start();
 	thread3.start();
 	thread4.start();
 	thread5.start();
 	thread6.start();
 	thread7.start();
 	thread8.start();
 	thread9.start();
 	thread10.start();

 	thread1.join();
 	thread2.join();
 	thread3.join();
 	thread4.join();
 	thread5.join();
 	thread6.join();
 	thread7.join();
 	thread8.join();
 	thread9.join();
 	thread10.join();
}

#endif /* TEST_THREAD_H_ */
